<?php

class QuoteusController extends Zend_Controller_Action {

    public function init() {

	/* Initialize action controller here */
    }

    public function getServiceArray() {
	return array(
	    '' => '-- Select Services --',
	    'mobile_application' => 'Mobile Application',
	    'web_application' => 'Web Application',
	    'game_development' => 'Game Development',
	    'independet_testing' => 'Independent Testing',
	    'seo_services' => 'Seo Services',
	    'others' => 'Others'
	);
    }

    public function indexAction() {
	global $config;

	$pubKey = $config->CAPTCHA_PUBLIC_KEY;
	$privKey = $config->CAPTCHA_PRIVATE_KEY;

	$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);
	$this->view->recaptcha = $recaptcha;

	$serviceArray = $this->getServiceArray();
	$quoteForm = new Form_QuoteForm();
	$quoteForm->services->addMultiOptions($serviceArray);

	$countryModel = new Model_Country();
	$countryArray = $countryModel->fetchCountry();
	$quoteForm->country->addMultiOptions($countryArray);

	$succesMeessage = "";
	$errorMessage = Array();
	if ($this->getRequest()->isPost()) {

	    if ($quoteForm->isValid($_POST)) {

		try {
		    $result = $recaptcha->verify(
				    $_POST['recaptcha_challenge_field'],
				    $_POST['recaptcha_response_field']
		    );
		    if (!$result->isValid()) {
			$quoteForm->addErrorMessage("Please Enter correct Captcha.");
			$errorMessage['0'] = $quoteForm->getErrorMessages();
		    } else {
			$fileName = '';
			$upload = new Zend_File_Transfer_Adapter_Http();
			foreach ($upload->getFileInfo() as $fields => $asFileInfo) {
			    if ($asFileInfo['name'] != '') {
				$upload->setDestination($config->UPLOAD_PATH . "quotations/");
				$originalFilename = pathinfo($asFileInfo['name']);
				$fileName = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $quoteForm->getValue('name')) . '_' . time() . '.' . $originalFilename['extension'];
				$upload->addFilter('Rename', $fileName, 'filename');
				$upload->receive($fields);
			    }
			}

			$quoteModel = new Model_Quote();
			// if the form is valid then create the new Quote
			$dataArray['name'] = $quoteForm->getValue('name');
			$dataArray['company'] = $quoteForm->getValue('company');
			$dataArray['email'] = $quoteForm->getValue('email');
			$dataArray['country'] = $quoteForm->getValue('country');
			$dataArray['phone'] = $quoteForm->getValue('phone');
			$dataArray['suitabletime'] = $quoteForm->getValue('suitabletime');
			$dataArray['services'] = $quoteForm->getValue('services');
			$dataArray['reqdetails'] = $quoteForm->getValue('reqdetails');
			$dataArray['filename'] = $fileName;
			$result = $quoteModel->createQuote($dataArray);


			$body = "";
			$body .= "Name : " . $quoteForm->getValue('name') . "<br/>";
			$body .= "Company Name: " . $quoteForm->getValue('company') . "<br/>";
			$body .= "Email : " . $quoteForm->getValue('email') . "<br/>";
			$body .= "Country  : " . $countryArray[$quoteForm->getValue('country')] . "<br/>";

			$body .= "Contact Number : " . $quoteForm->getValue('phone') . "\r<br/>";
			$body .= "Suitable time to call: " . $quoteForm->getValue('suitabletime') . "\r<br/>";
			$body .= "Services: " . $serviceArray[$quoteForm->getValue('services')] . "\r<br/>";
			$body .= "Details about requirement: " . $quoteForm->getValue('reqdetails') . "\r<br/>";

			$mail = new Zend_Mail();
			$mail->setBodyText($body);
			$mail->setBodyHtml($body, "iso-8859-1");


			$mail->addTo('ketan.jems@gmail.com', 'Ketan Patel');
			$mail->addTo('vyasishanatc194@gmail.com', 'Vyas Ishan');
			$mail->addTo('info@citrusbug.com', 'Info Citrus Bug');
			$mail->setFrom('info@citrusbug.com', 'Info Citrus Bug');
			$mailSubject = "Quote form Inquiry to CitrusBug by [". $quoteForm->getValue('name')."]";
			$mail->setReplyTo('info@citrusbug.com', 'Citrus Bug');
			$mail->addHeader('MIME-Version', '1.0');
			$mail->addHeader('Content-Transfer-Encoding', '8bit');
			$mail->addHeader('X-Mailer:', 'PHP/'.phpversion());
			$mail->setSubject($mailSubject);

			// add attachment
			if ($fileName != '') {
			    $fileContents = file_get_contents($config->UPLOAD_PATH . "quotations/" . $fileName);
			    $resume = $mail->createAttachment($fileContents);
			    $resume->filename = $fileName;
			}
			$mail->send();
			if ($result > 1) {
			    $quoteForm->reset();
			    $succesMeessage = "Thanks for sending quote to us! Will be in touch soon.";
			}
		    }
		} catch (Exception $e) {
		    $quoteForm->addErrorMessage("Please Enter Captcha.");
		    $errorMessage['0'] = $quoteForm->getErrorMessages();
		}
	    } else {
		$errorMessage = $quoteForm->getMessages();
	    }
	}

	$this->view->headTitle('Citrusbug Quote Us - Request a proposal');
	$this->view->form = $quoteForm;
	$this->view->errorMessage = $errorMessage;
	$this->view->succesMeessage = $succesMeessage;
	
	$this->view->showMetaDescription = TRUE;
	$this->view->showMetaKeyword = TRUE;
	$this->view->META_DESCRIPTION = "Here you can request a proposal to citrusbug, as soon we get your request we wiill get touch with you.";
	$this->view->META_KEYWORDS = "web development, web design, mobile application development, android application, PHP5, CSS3, PHP web developement";
	
	
	
	
    }

}

?>